//
//  FTYTabBarController.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/26.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "FTYTabBarController.h"

#import "FTYNavigationController.h"
#import "RWHomePageViewController.h"
#import "RWCategoryViewController.h"
#import "RWCollectionViewController.h"
#import "RWShoppingCart2ViewController.h"
#import "RWMyViewController.h"

#define TabCount 5

@interface FTYTabBarController ()

@property (nonatomic,strong) UIButton *selectedeButton;

@end

@implementation FTYTabBarController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addControllers];
    [self initObjectsContent];
    [self setTabBarShape];
    [self setTabBarContent];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Invoke SetTabBar Method

- (void)addControllers{

    RWHomePageViewController *homePageVC = [[RWHomePageViewController alloc] init];
    FTYNavigationController *homePageVNC = [[FTYNavigationController alloc] initWithRootViewController:homePageVC];
    
    RWCategoryViewController *categoryVC = [[RWCategoryViewController alloc] init];
    FTYNavigationController *categoryVNC = [[FTYNavigationController alloc] initWithRootViewController:categoryVC];
    
    RWCollectionViewController *collectionVC = [[RWCollectionViewController alloc] init];
    FTYNavigationController *collectionVNC = [[FTYNavigationController alloc] initWithRootViewController:collectionVC];
    
    RWShoppingCart2ViewController *shoppingCartVC = [[RWShoppingCart2ViewController alloc] init];
    FTYNavigationController *shoppingCartVNC = [[FTYNavigationController alloc] initWithRootViewController:shoppingCartVC];
    
    RWMyViewController *myVC = [[RWMyViewController alloc] init];
    FTYNavigationController *myVNC = [[FTYNavigationController alloc] initWithRootViewController:myVC];
    
    self.viewControllers = [NSArray arrayWithObjects:homePageVNC, categoryVNC, collectionVNC, shoppingCartVNC, myVNC, nil];
    
}

- (void)initObjectsContent{
    
    _selectedImageArray = [[NSArray alloc] initWithObjects:@"home_sel_icon", @"fenle_sel_icon", @"focus_sel_icon", @"shop_sel_icon", @"my_sel_icon", nil];
    _unSelectedImageArray = [[NSArray alloc] initWithObjects:@"home_icon", @"fenlei_icon", @"focus_icon",@"shopping_icon", @"my_icon",  nil];
    _tabBarTitleArray = [[NSArray alloc] initWithObjects:@"首页", @"分类", @"关注", @"购物车", @"个人中心", nil];
}

- (void)setTabBarShape{
    
    _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_TABBAR_HEIGHT)];
    [self.tabBar addSubview:_tabBarView];
    _tabBarView.backgroundColor = [UIColor whiteColor];
    _tabBarView.userInteractionEnabled = YES;//默认为YES
}

- (void)setTabBarContent{
    
    for (int i=0; i<TabCount; i++) {
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        //bt.showsTouchWhenHighlighted = YES;
        float itemWidth = FTY_SCREEN_WIDTH / TabCount;
        bt.frame = CGRectMake((i*itemWidth), 0, itemWidth, FTY_TABBAR_HEIGHT) ;
        bt.tag = 19980+i;
        [bt addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
        [_tabBarView addSubview:bt];
        
        UIImageView * imge = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 27*FTY_WIDTH_BASE, 27*FTY_HEIGHT_BASE)];
        [bt addSubview:imge];
        imge.userInteractionEnabled = NO;
        imge.center = CGPointMake(bt.frame.size.width / 2, bt.frame.size.height / 2);
        imge.tag = 19960+i;
        imge.image = [UIImage imageNamed:[_unSelectedImageArray objectAtIndex:i]];
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 27*FTY_WIDTH_BASE, 27*FTY_HEIGHT_BASE)];
        [bt addSubview:lab];
        lab.text = _tabBarTitleArray[i];
        lab.textColor = [FTYColor whiteColor];
        lab.font = FTY_FONTTSIZE(14);
        lab.tag = 19940+i;
        if (i == 0) {
            bt.selected = YES;
            _selectedeButton = bt;
            NSString * SelectedImageStr = [_selectedImageArray objectAtIndex:i];
            imge.image = [UIImage imageNamed:SelectedImageStr];
        }
    }
}

- (void)selectedTab:(UIButton *)button {
    
    //变成默认的
    NSInteger nomorTag = _selectedeButton.tag - 19980;
    UIImageView *nomorImage = (UIImageView *)[self.view viewWithTag:19960+nomorTag];
    nomorImage.image = [UIImage imageNamed:[_unSelectedImageArray objectAtIndex:nomorTag]];
    
    //变成选中的
    NSInteger selectedTag = button.tag - 19980;
    UIImageView *vv = (UIImageView *)[self.view viewWithTag:19960+selectedTag];
    vv.image = [UIImage imageNamed:[_selectedImageArray objectAtIndex:selectedTag]];
    _selectedeButton = button;
    self.selectedIndex = button.tag-19980;
}


@end
